package com.zack.gank.gankandroid.main.main;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gank.zack.ganklibrary.view.staggeredgrid.StaggeredGridView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.zack.gank.gankandroid.gankandroid.R;
import com.zack.gank.gankandroid.main.BaseFragment;
import com.zack.gank.gankandroid.main.find.bean.Gank;
import com.zack.gank.gankandroid.main.find.bean.ImageGank;
import com.zack.gank.gankandroid.main.main.adapter.SeaxyAdapter;
import com.zack.gank.gankandroid.main.main.adapter.SeaxyGridAdapter;
import com.zack.gank.gankandroid.utils.InterfaceUtils;
import com.zack.gank.gankandroid.utils.StringUtils;

import java.util.ArrayList;

/**
 * Created by Android on 2017/7/12.
 */
public class SexyFragment extends BaseFragment implements SexyView{

    private RefreshLayout refreshLayout;
    private RecyclerView rlvSexy;
    private SexyPresenter sexyPresenter;
    private int number=1;
    private StaggeredGridView gridView;
//    ArrayList<Gank> ganks=new ArrayList<>();
    private SeaxyAdapter seaxyAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fg_sexy, container,false);
        refreshLayout= (RefreshLayout) view.findViewById(R.id.refreshLayout);
        rlvSexy= (RecyclerView) view.findViewById(R.id.rlv_sexy);
//        gridView= (StaggeredGridView) view.findViewById(R.id.grid_view);
        /*//设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                //指定为经典Header，默认是 贝塞尔雷达Header
                return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });*/

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        loadData();


        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshLayout) {
                number++;
                loadData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                loadData();
            }
        });
    }

    private void loadData(){
        if(sexyPresenter==null){
            sexyPresenter=new SexyPresenter(this);
        }
        sexyPresenter.setGankNumPager(InterfaceUtils.typeseaxyData, StringUtils.imagenumber,Integer.toString(number));
    }

    @Override
    public void onSucess(final ImageGank imageGank) {


        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadmore();
        /*if(number==1){
            ganks.clear();
        }*/

                if (imageGank != null) {
                    final StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
//            manager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
                    rlvSexy.addOnScrollListener(new RecyclerView.OnScrollListener() {
                        @Override
                        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                            super.onScrollStateChanged(recyclerView, newState);
                            manager.invalidateSpanAssignments();
                        }

                        @Override
                        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                            super.onScrolled(recyclerView, dx, dy);
                        }
                    });
                    rlvSexy.setLayoutManager(manager);
//            ganks.addAll(imageGank.results);
                    if (seaxyAdapter == null) {
                        seaxyAdapter = new SeaxyAdapter(getContext(), imageGank.results);
                        rlvSexy.setAdapter(seaxyAdapter);
                    } else {
                        seaxyAdapter.addListData(imageGank.results);
//                seaxyAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    @Override
    public void onFile(Throwable t) {

    }
}
